package com.github.duanyashu;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.duanyashu.listener.EasyExcelListener;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @description: 处理工具类
 * @author: duanyashu
 * @time: 2020-07-10 09:47
 */
public class EasyExcelUtils {

    /**
     * 导出excel方法
     *
     * @param outputStream
     * @param objects  导出的数据
     * @param clazz    导出的对象类型
     * @param sheetName sheet名称
     */
    public static void webWriteExcel(OutputStream outputStream, List objects, Class clazz, String sheetName) {
        webWriteExcel(outputStream, objects, clazz, null, sheetName);
    }

    /**
     * 带错误信息导出excel方法
     * @param outputStream
     * @param objects 导出的数据
     * @param clazz 导出的对象类型
     * @param errMsgList 导出的错误信息
     * @param sheetName sheet名称
     */
    public static void webWriteExcel(OutputStream outputStream,List objects, Class clazz, List<Map<Integer, String>> errMsgList,  String sheetName) {
        //标题样式
        WriteCellStyle headCellStyle = new WriteCellStyle();
        headCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //数据样式
        WriteCellStyle contentCellStyle = new WriteCellStyle();
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headCellStyle, contentCellStyle);
        try {
            ExcelWriterBuilder write = EasyExcel.write(outputStream, clazz);
            if (errMsgList != null) {
                //inMemory(Boolean.TRUE)开启批注   批注在ErrorSheetWriteHandler中实现
                write.inMemory(Boolean.TRUE)
                        .registerWriteHandler(new ErrorSheetWriteHandler(errMsgList));
            }
            write.registerWriteHandler(horizontalCellStyleStrategy).sheet(sheetName).doWrite(objects);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * excel导入方法
     *
     * @param fileInputStream 导入文件
     * @param clazz           对应的实体类
     * @return 处理监听器
     */
    public static EasyExcelListener webImportExcel(InputStream fileInputStream, Class clazz){
        return webImportExcel(fileInputStream, null, clazz);
    }



    /**
     * excel导入方法
     *
     * @param fileInputStream    导入文件
     * @param customCheckService 自定义校验服务 service接口继承 ExcelCheckManager实现自主校验
     * @param clazz              对应的实体类
     * @return 处理监听器 可以获取错误数据
     */
    public static EasyExcelListener webImportExcel(InputStream fileInputStream, ExcelCheckManager customCheckService, Class clazz) {
        EasyExcelListener easyExcelListener = new EasyExcelListener(customCheckService, clazz);
        EasyExcel.read(fileInputStream, clazz, easyExcelListener).sheet().doRead();
        return easyExcelListener;
    }


}
